<?php
namespace Admin\Controller;
use Think\Controller;
    class ShopBaseController extends  IndexController{
         public function lists(){
             $type=I("request.type");
             if($type==1){
                 $shopBase=D("Shop_base");
                 $data['lib']=I("post.lib");
                 $data['sid']=I("post.sid");
                 $data['name'] =I("post.name");
                 $data['en_name']=I("post.en_name");
                 $data['phone']=I("post.phone");
                 $data['cn_abstract']=I("post.cn_abstract");
                 $data['en_abstract']=I("post.en_abstract");
                 $data['addr']=I("post.addr");
                 $data['standards']=I("post.standards");
                 $data['life_description']=I("post.life_description");
                 $data['characteristic']=I("post.characteristic");
                 $data['gift']=I("post.gift");
                 $data['not_contain']=I("post.not_contain");
                 $data['zid']=I("post.zid");
                 $data['cid']=I("post.cid");
                 $data['bz']=I("post.bz");
                 $data['is_hz']=I("post.is_hz");
                 $data['is_new']=I("post.is_new");
                 $data['pay_percentage']=I("post.pay_percentage");
                 $data['u_poundage']=I("post.u_poundage");
                 $data['information']=I("post.information");
                 $data["username"]=I("post.username");
                 $data['bh_start']=I("post.bh_start");
                 $data['bh_stop']=I("post.bh_stop");
                 $data['is_bh']=I("post.is_bh");
                 $data['lat']=I("post.lat");
                 $data['lng']=I("post.lng");
                 $data['remark']=I("post.remark");
                 $data['create_time']= time();
                 if (is_array($_FILES['imgs'][0])||$_FILES['imgs'][0]!=null) {
                     //=================处理多图片上传==================
                     header("content-type:text/html;charset=utf-8");
                     $file = $_FILES;
                     $newFile = array();
                     foreach ($file as $key => $value) {
                         $num = count($value['name']);
                         for ($i = 0; $i < $num; $i++) {
                             $newFile[$i]['name'] = $value['name'][$i];
                             $newFile[$i]['type'] = $value['type'][$i];
                             $newFile[$i]['tmp_name'] = $value['tmp_name'][$i];
                             $newFile[$i]['error'] = $value['error'][$i];
                             $newFile[$i]['size'] = $value['size'][$i];
                         }
                     }
                     header("content-type:text/html;charset=utf-8");
                     $upload = new \Think\Upload(); // 实例化上传类
                     $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
                     $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                     $upload->rootPath = "./imageShopBase/";
                     $upload->replace = true; //存在同名文件是否是覆盖
                     $upload->autoSub = true;  // 是否使用子目录保存上传文件
                     $info = $upload->upload($newFile);
                     if (!$info) {
                         $data['imgs'] = "";
                     } else {
                         $path = '';
                         for ($i = 0; $i <= count($info) - 1; $i++) {
                             $path .= ltrim($info[$i]['savepath'], './') . $info[$i]['savename'] . ',';
                             $data['imgs'] = $path;
                         }
                     }
                 }

                 if ($_FILES['logo_img'] !== null) {
                     header("content-type:text/html;charset=utf-8");
                     $upload = new \Think\Upload(); // 实例化上传类
                     $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
                     $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型                     $upload->rootPath = "./imageShopBaseLog/";
                     $upload->replace = true; //存在同名文件是否是覆盖
                     $upload->autoSub = true; // 是否使用子目录保存上传文件
                     $logoInfo = $upload->uploadOne($_FILES['logo_img']);
                     if (!$logoInfo) {
                         $data['logo_img'] = "";
                     } else {
                         //拼接上传的图片路径用于上传到服务器
                         $logoPath = '';
                         $logoPath .= ltrim($logoInfo['savepath'], './') . $logoInfo['savename'];
                         $data['logo_img'] = $logoPath;
                     }
                 }
                       $shopBase->add($data);
                       $this->redirect('ShopBase/lists');
             }
                 $this->display();
        }

        //==================================获得商铺列表=======================================
        public function get_shopBase_list(){
            $shopBase = D("Shop_base");
            $shopClass = D("Shop_class");
            $country=D('Country');
            $state= D("state");
            $result = $shopBase->select();
            foreach($result as $key=>&$value){
                $value['lid']=$shopClass->where('id='.$value['lid'])->getField("name");
                $value['cid']=$country->where('countryid='.$value['cid'])->getField("countryname_cn");
                if($value['zid']!=null){
                    $value['zid']=$state->where("id=".$value['zid'])->getField("name");
                }
                $value['is_hz']=$value['is_hz']==1?'合作商家':'普通商家';
                $value['is_bh']=$value['is_bh']==1?'全日开放':'非全日开放';
                $value['is_new']= $value['is_new']==1? '新店':'非新店';
            }
            if(!$result){
                echo json_encode(0);exit;
             }
                echo json_encode($result);exit;
             }
        //=========================用于显示数据添加页面的视图=================================
        public  function add(){
           $this->display();
        }
        //=====================================获取商铺类别======================================
        public function get_shop_class(){
            $shopClass =D("shopClass");
            $result=$shopClass->field('id,name')->select();
            if(!$result){
                echo json_encode(0);exit;
            }
                echo json_encode($result);exit;
        }
        //================================国家列表==========================
        public function country_list()
        {
            $Country = D("Country");
            $r = $Country->select();
            if (!$r) {
                echo json_encode(0);
                exit;
            }
            echo json_encode($r);
            exit;
        }

        //============================城市列表==================================
        public function city_list(){
            $cid = I("request.cid");
            $State = D("State");
            $r = $State->where('cid=' . $cid)->select();
            if (!$r) {
                echo json_encode(0);exit;
            }
            echo json_encode($r);exit;
        }
        //====================----用于显示修改视图的呈现--=========================
        public function update(){
            $id=I("request.id");
            setcookie("uid", $id, time()+3600);
            $this->display();
        }
        //========================获取要修改的数据========================================
        public function update_list(){
            $id=$_COOKIE["uid"];
            $shopBase=D("Shop_base");
            $result=$shopBase->where("id=".$id)->find();
            if(!$result){
                echo json_encode(0);exit;
            }
                echo json_encode($result);exit;
        }

    //=================================获取要修改的图片===================================================
        public function get_imgs_list(){
            $id=$_COOKIE["uid"];
            $shopBase=D("Shop_base");
            $imgsWhere="id=".$id;
            $result= $shopBase->where($imgsWhere)->getField("imgs");
            $str=rtrim($result,',');
            $arr= explode(',',$str);
            if(!$arr){
                echo json_encode(0);exit;
            }
            echo json_encode($arr);exit;
        }
        //======================当删除从数据库取出的某一张照图片时===================
        public function update_imgs(){
            $id=$_COOKIE["uid"];
            $imgNum = I("request.imgNum");
            $shopBase=D("Shop_base");
            $where="id=".$id;
            $imgs = $shopBase->where($where)->getField('imgs');
            $str = str_replace($imgNum . ',', "", $imgs);
            $data['imgs'] = $str;
            unlink("./imageShopBase/" . $imgNum);
            $where="id=".$id;
            $shopBaselResult = $shopBase->where($where)->save($data);
            if (!$shopBaselResult) {
                echo json_encode(0); exit;
            } else {
                echo json_encode($shopBaselResult); exit;
            }
        }

        //============================数据修改时呈现得国家信息==========================
        public function get_country_list(){
            $country=D('country');
            $result= $country->field("countryid,countryname_cn")->select();
            if(!$result){
                echo json_encode(0);exit;
            }
            echo json_encode($result);exit;
        }

       //============================数据修改时呈现的信息===========================
        public function get_city_one(){
            $zid=I("request.zid");
            $state=D('State');
            $result=$state->where('id='.$zid)->field('id,name')->find();
            if(!$result){
                echo json_encode(0);exit;
            }
            echo json_encode($result);exit;
        }
        //==============================数据修呈现的国家信息时地区的变化============================
        public function get_city_list(){
            $cid=I("request.cid");
            $state=D('state');
            $result= $state->where("cid=".$cid)->field("id,name")->select();
            if(!$result){
                echo json_encode(0);exit;
            }
            echo json_encode($result);exit;

        }

        //============================------更新数据-------===================================
        // 更新数据
        public function actionUpdate(){
            $shopBase=D("Shop_base");
            $data['lib']=I("post.lib");
            $data['sid']=I("post.sid");
            $data['name'] =I("post.name");
            $data['en_name']=I("post.en_name");
            $data['phone']=I("post.phone");
            $data['cn_abstract']=I("post.cn_abstract");
            $data['en_abstract']=I("post.en_abstract");
            $data['addr']=I("post.addr");
            $data['standards']=I("post.standards");
            $data['life_description']=I("post.life_description");
            $data['characteristic']=I("post.characteristic");
            $data['gift']=I("post.gift");
            $data['not_contain']=I("post.not_contain");
            $data['zid']=I("post.zid");
            $data['cid']=I("post.cid");
            $data['bz']=I("post.bz");
            $data['is_hz']=I("post.is_hz");
            $data['is_new']=I("post.is_new");
            $data['pay_percentage']=I("post.pay_percentage");
            $data['u_poundage']=I("post.u_poundage");
            $data['information']=I("post.information");
            $data["username"]=I("post.username");
            $data['bh_start']=I("post.bh_start");
            $data['bh_stop']=I("post.bh_stop");
            $data['is_bh']=I("post.is_bh");
            $data['lat']=I("post.lat");
            $data['lng']=I("post.lng");
            $data['remark']=I("post.remark");
            $data['update_time']= time();;
            if ($_FILES['logo_img']!= null) {
                if (isset($_COOKIE["uid"])) {
                    $r = $shopBase->where('id=' . $_COOKIE["uid"])->find();
                    if ($r['logo_img'] != null) {
                        unlink("./imageShopBaseLog/" . $r['logo_img']);
                    }
                }
                header("content-type:text/html;charset=utf-8");

                $upload = new \Think\Upload(); // 实例化上传类

                $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
                $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath ="./imageShopBaseLog/";
                $upload->replace = true; //存在同名文件是否是覆盖
                $upload->autoSub = true; // 是否使用子目录保存上传文件
                // 采用date函数生成命名规则 传入Y-m-d参数
                $upload->saveName = time() . '_' . mt_rand();
                $info = $upload->uploadOne($_FILES['logo_img']);
                if (!$info) {
                    $data['logo_img'] = '';
                } else {
                    $data['logo_img'] = ltrim($info['savepath'] . $info['savename'], '/');
                }
            }

            if ($_FILES['imgs']['name'][0] != null) {
                $file = $_FILES;
                $newFile = array();
                foreach ($file as $key => $value) {
                    $num = count($value['name']);
                    for ($i = 0; $i < $num; $i++) {
                        $newFile[$i]['name'] = $value['name'][$i];
                        $newFile[$i]['type'] = $value['type'][$i];
                        $newFile[$i]['tmp_name'] = $value['tmp_name'][$i];
                        $newFile[$i]['error'] = $value['error'][$i];
                        $newFile[$i]['size'] = $value['size'][$i];
                    }
                }
                header("content-type:text/html;charset=utf-8");

                $upload = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
                $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = "./imageShopBase/";
                $upload->replace = true; //存在同名文件是否是覆盖
                // 是否使用子目录保存上传文件
                $upload->autoSub = true;
                $info = $upload->upload($newFile);
                if (!$info) {
                    $data['imgs'] = "";
                } else {
                    //拼接上传的图片路径用于上传到服务器
                        $path = '';
                        for ($i = 0; $i <= count($info) - 1; $i++) {
                            $path .= ltrim($info[$i]['savepath'], './') . $info[$i]['savename'] . ',';
                        }
                        if (isset($_COOKIE["uid"])) {
                            $getData = $shopBase->where("id=" . $_COOKIE["uid"])->getField('imgs');
                        }

                        if ($path != null) {
                            $data['imgs'] = $path . $getData;
                        } else {
                            $data['imgs'] = $getData;
                        }
                }
            }

            if(isset($_COOKIE["uid"])) {
                $shopBase->where("id=" . $_COOKIE["uid"])->save($data);
                $this->redirect("ShopBase/lists");
            }
        }

    }
